using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbDoanhNghiepDAL
    {
        private SqlConnection cn = null;
        public tbDoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbDoanhNghiepDAL(tbDoanhNghiepDAL tuan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_tbDoanhNghiep_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDN", doanhnghiep.TenDN);
            cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
            cm.Parameters.AddWithValue("@Email", doanhnghiep.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", doanhnghiep.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", doanhnghiep.MatKhau);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_tbDoanhNghiep_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", doanhnghiep.MaDN);
            cm.Parameters.AddWithValue("@TenDN", doanhnghiep.TenDN);
            cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
            cm.Parameters.AddWithValue("@Email", doanhnghiep.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", doanhnghiep.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", doanhnghiep.MatKhau);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbDoanhNghiep_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", iD);
            cm.ExecuteNonQuery();
        }
        public tbDoanhNghiepCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbDoanhNghiep_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDoanhNghiepCollection dnCollection = new tbDoanhNghiepCollection();

            while (dr.Read())
            {
                tbDoanhNghiep doanhnghiep = new tbDoanhNghiep();
                doanhnghiep.MaDN = int.Parse(dr.GetValue(0).ToString());
                doanhnghiep.TenDN = dr.GetValue(1).ToString();
                doanhnghiep.DiaChi = dr.GetValue(2).ToString();
                doanhnghiep.DienThoai = dr.GetValue(3).ToString();
                doanhnghiep.Email = dr.GetValue(4).ToString();
                doanhnghiep.TaiKhoan = dr.GetValue(5).ToString();
                doanhnghiep.MatKhau = dr.GetValue(6).ToString();
                dnCollection.Add(doanhnghiep);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dnCollection;
        }
    }
}
